﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace FlyBird
{
    class Bird
    {
        //小鸟矩形对象
        private Rectangle rect;

        public Rectangle _rect
        {
            get { return rect; }
            set { rect = value; }
        }

        //小鸟的加速度
        private int acceleration;

        //小鸟的存活状态
        public bool life = true;


        //存放小鸟的图片
        private Image[] images;
        //小鸟图片的数量
        public static readonly int BIRD_IMG_COUNT = 3;
        //鸟的状态
        private int state;
        public const int STATE_NORMAL = 0;//平着飞
        public const int STATE_UP = 1;//向上飞
        public const int STATE_DOWN = 2; //向下飞

        //小鸟的位置
        private int x = 200, y = 200;

        //小鸟移动方向 上下
        private bool up = false;


        //构造函数
        public Bird()
        {
            images = new Image[BIRD_IMG_COUNT];
            for (int i = 0; i < BIRD_IMG_COUNT; i++)
            {
                images[i] = GameUtil.loadBufferedImage(StaticValue.BIRD_IMG[i]);
            }
            int w = images[0].Width;
            int h = images[0].Height;
            //初始化小鸟的矩形对象
            rect = new Rectangle(x, y, w, h);
        }

        //绘制小鸟
        public void draw(Graphics g)
        {
            flyLogic();
            g.DrawImage(images[state], x, y);

            //绘制小鸟矩形
            Pen pen = new Pen(Color.Red);
            rect.X = x;
            rect.Y = y;
            //g.DrawRectangle(pen, rect);
        }

        //控制小鸟移动方向的逻辑
        public void flyLogic()
        {
            if (up)
            {
                //每次加速度+1
                acceleration--;
                //移动
                y += acceleration;
                //速度上限
                if (acceleration < -7)
                {
                    acceleration = -7;
                }
                //飞到顶端
                if (y < 20)
                {
                    y = 20;
                    acceleration = 0;
                }
            }
            if(!up)
            {
                acceleration++;
                y += acceleration;
                if (acceleration > 7)
                {
                    acceleration = 7;
                }
                //飞到底部
                if (y > 375)
                {
                    y = 375;
                    acceleration = 0;
                }
            }
        }
        //小鸟飞行
        public void fly(int fly)
        {
            switch (fly)
            { 
                case STATE_UP:
                    state = 1;
                    up = true;
                    break;
                case STATE_DOWN:
                    state = 2;
                    up = false;
                    break;

            }
        }
        //重新绘制小鸟的位置
        public void restartDraw()
        {
            life = true;
            x = 200;
            y = 200;
        }


    }
}
